package com.zkingsoft.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.zkingsoft.bean.WebArticleType;
import com.zkingsoft.common.tools.ServiceUtil;
import com.zkingsoft.dao.WebArticleTypeDao;
import com.zkingsoft.service.WebArticleTypeService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-12-01 10:36
 */
@Service("webArticleTypeService")
public class WebArticleTypeServiceImpl implements WebArticleTypeService {

	@Autowired
	private WebArticleTypeDao webArticleTypeDao;

	@Autowired
	private ServiceUtil serviceUtil;

	private String tableName = "web_article_type";

	@Override
	public int add(WebArticleType webArticleType) {
		if (serviceUtil.addCheckRepeat(tableName, "code", webArticleType.getCode())) {
			throw new GlobleException("code值已经存在");
		}
		return webArticleTypeDao.insert(webArticleType);

	}

	@Override
	public int batchAdd(List<WebArticleType> webArticleTypeList) {
		return webArticleTypeDao.batchInsert(webArticleTypeList);
	}

	@Override
	public int modifyByMap(WebArticleType oldWebArticleType, WebArticleType newWebArticleType) {
		if (serviceUtil.updateCheckRepeat(tableName, "code", newWebArticleType.getCode(), "art_type_id",
				oldWebArticleType.getArtTypeId())) {
			throw new GlobleException("code值已经存在");
		}
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldWebArticleType, newWebArticleType)) {
				return 1;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldWebArticleType, newWebArticleType);
		} catch (Exception e) {
			new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("artTypeId", oldWebArticleType.getArtTypeId());
			webArticleTypeDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(WebArticleType webArticleType) {

		return webArticleTypeDao.updateByModel(webArticleType);

	}

	@Override
	public int remove(List<Long> list) {

		return webArticleTypeDao.deleteByIds(list);

	}

	/**
	 * 删除该文章类型及其子类
	 */
	@Override
	public int removeById(Long artTypeId) {
		if (artTypeId == null) {
			return 0;
		}
		List<Long> ids = new ArrayList<Long>();
		ids = getArtTypeChirds(artTypeId, new ArrayList<Long>());
		return webArticleTypeDao.deleteByIds(ids);

	}

	@Override
	public int removeByModel(WebArticleType webArticleType) {

		return webArticleTypeDao.deleteByModel(webArticleType);

	}

	@Override
	public List<WebArticleType> findInPage(WebArticleType webArticleType, PaginationVO pageVo) {

		return webArticleTypeDao.selectInPage(webArticleType, pageVo);

	}

	@Override
	public List<WebArticleType> findByModel(WebArticleType webArticleType) {

		return webArticleTypeDao.selectByModel(webArticleType);

	}

	@Override
	public int findTotal(WebArticleType webArticleType) {

		return webArticleTypeDao.selectTotalRecord(webArticleType);

	}

	@Override
	public WebArticleType findById(Long artTypeId) {

		return webArticleTypeDao.selectById(artTypeId);

	}

	/**
	 * 获取文章类型下的子类型id，包含功能自己的id
	 * 
	 * @param id
	 * @param ids
	 * @return
	 */
	private List<Long> getArtTypeChirds(Long id, List<Long> ids) {
		WebArticleType webArticleType = new WebArticleType();
		webArticleType.setArtTypeParentId(id);
		// 获取子节点
		List<WebArticleType> list = webArticleTypeDao.selectByModel(webArticleType);
		if (list.size() > 0) {
			for (WebArticleType articleType : list) {
				getArtTypeChirds(articleType.getArtTypeId(), ids);
			}
		}
		ids.add(id);
		return ids;
	}

}